﻿using System;
using System.Management.Automation;

using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;

namespace Cognifide.PowerShell.Shell.Commands
{
    //[Cmdlet("Ask", "Question", SupportsShouldProcess = true, DefaultParameterSetName = "Name")]
    public class AskQuestionCommand : BaseCommand
    {
        [ValidatePattern("[\\*\\?\\[\\]\\-0-9a-zA-Z_]+")]
        [Parameter(ValueFromPipeline = true, Position = 0)]
        public string Name { get; set; }

        [Parameter(ValueFromPipeline = true)]
        public Item Item { get; set; }

        protected override void ProcessRecord()
        {
            if (Item != null)
            {
                System.Collections.Specialized.NameValueCollection parameters = new System.Collections.Specialized.NameValueCollection();
                parameters["id"] = Item.ID.ToString();
                parameters["language"] = Item.Language.ToString();
                parameters["database"] = Item.Database.Name;
                Sitecore.Context.ClientPage.Start(this, "Run", parameters);
            }
        }

        protected void Run(Sitecore.Web.UI.Sheer.ClientPipelineArgs args)
        {
            if (args.IsPostBack)
            {
                if ((!String.IsNullOrEmpty(args.Result)) && args.Result != "undefined" && args.Result != "null")
                {
                    Database db = Factory.GetDatabase(args.Parameters["database"]);
                    Item myItem = db.GetItem(new ID(args.Parameters["id"]));
                }
            }
            else
            {

                Sitecore.Context.ClientPage.ClientResponse.Input("My Message:"
                    , "My Default Value"
                    , Sitecore.Configuration.Settings.ItemNameValidation
                    , "'$Input' is not a valid name.", 255);
                args.WaitForPostBack();
            }
        }  
    }
}

